<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="func" uri="http://www.ecmp.com/tag" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ tag pageEncoding="UTF-8" %>

<%@ attribute name="tableName" required="true" rtexprvalue="true" description="" %>
<%@ attribute name="key" rtexprvalue="true" description="" %>
<%@ attribute name="value" rtexprvalue="true" description="" %>
<%@ attribute name="css" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="whereConditon" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="keyFieldName" required="true" rtexprvalue="true" description="" %>
<%@ attribute name="valueFieldName" required="true" rtexprvalue="true" description="" %>
<%@ attribute name="defaultKey" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="defaultValue" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="isDisplayKey" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="onchange" required="false" rtexprvalue="true" description="" %>
<%@ attribute name="placeholder" required="false" rtexprvalue="true" description="" %>


<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<input type="hidden" value="${defaultKey}" id="${keyFieldName}" name="${keyFieldName}" datafield="SearchTable.${keyFieldName}">
<input type="text" value="${defaultValue}" id="${valueFieldName}" name="${valueFieldName}">


<script>
    var _split = "------===";
    var columns = [];
    $(document).ready(function () {
        if (${not empty valueEn}) {
            columns = ['编码', '中文名称', '英文名称'];
        } else {
            columns = ['编码', '中文名称'];
        }
        var txt${valueFieldName} = $("#${valueFieldName}").tautocomplete({
            relateKey: "${keyFieldName}",
            placeholder: "${placeholder}"==""?"请输入关键字":"${placeholder}",
            width: "400px",
            css: "${css}",
            columns: columns,
            regex: "^[a-zA-Z0-9\\s]+$",
            ajax: {
                url: "${basePath}/cache/getBlurDict",
                type: "POST",
                data: function () {
                    var searchData = txt${valueFieldName}.searchdata();
                    var whereConditon = "${whereConditon}";
                    if (${"berthCode" == keyFieldName} && $("#portCode").val() != "" && $("#portCode").val() != null) {
                        whereConditon = " AND PORT_CODE='" + $("#portCode").val() + "'";
                    }else if(${"currentBerthCode" == keyFieldName} && $("#currentPortCode").val() != "" && $("#currentPortCode").val() != null){
                        whereConditon = " AND PORT_CODE='" + $("#currentPortCode").val() + "'";
                    }else if(${"moveToBerthCode" == keyFieldName} && $("#moveToPortCode").val() != "" && $("#moveToPortCode").val() != null){
                        whereConditon = " AND PORT_CODE='" + $("#moveToPortCode").val() + "'";
                    }else if(${"planBerth" == keyFieldName} && $("#portCode").val() != "" && $("#portCode").val() != null){
                        whereConditon = " AND PORT_CODE='" + $("#portCode").val() + "'";
                    }
                    return {
                        "name_startsWith": searchData,
                        "tableName": '${tableName}',
                        "maxRows": '11',
                        "key": '${key}'==''?'ITEM_CODE':'${key}',
                        "value": '${value}'==''?'ITEM_NAME':'${value}',
                        "valueEn": '${valueEn}',
                        "whereConditon": whereConditon,
                        "groupId": "${groupId}"
                    };
                },
                success: function (jsonData) {
                    var filterData = [];
                    $.each(jsonData, function (i, v) {
                        var obj = new Object();
                        obj.id = v['${value}'==''?'ITEM_NAME':'${value}'] + _split + v['${key}'==''?'ITEM_CODE':'${key}'] + _split + v['${valueEn}'];

                        obj.KEY = v['${key}'==''?'ITEM_CODE':'${key}'];
                        obj.VALUE = v['${value}'==''?'ITEM_NAME':'${value}'];
                        if (typeof(v['${valueEn}']) == "undefined") {
                            obj.VALUE2 = '';
                        } else {
                            obj.VALUE2 = v['${valueEn}'];
                        }
                        filterData.push(obj);
                    });
                    return filterData;
                }
            },
            onchange: function () {
                var datas = txt${valueFieldName}.id().split(_split);
                var value = datas[0] || '';
                var key = datas[1] || '';
                var valueEn = datas[2] || '';
                $("#${keyFieldName}").val(key);
                $("#${valueFieldName}").val(value);

                var el = txt${valueFieldName};
                var displayTxt = "";
                if (${not empty valueEn}) {
                    displayTxt = ${'true' == isDisplayKey} ? key : (key + "/" + value + "/" + valueEn);
                } else {
                    displayTxt = ${'true' == isDisplayKey} ? key : (key + "/" + value);
                }
                displayTxt = (displayTxt == '/' || displayTxt == '//') ? '' : displayTxt;
                el.ddTextbox.data("text", displayTxt);
                el.ddTextbox.val(displayTxt);

                if (${not empty relateKeyFieldName}) {
                    $("#${relateKeyFieldName}").val(key);
                }

                if (${not empty relateValueFieldName}) {
                    $("#${relateValueFieldName}").val(value);
                }

                if (${not empty onchange}) {
                    ${onchange}(key, value);
                }
                return;
            }
        });

        var displayTxt = ${'true' == isDisplayKey} ? '${defaultValue}' : ('${defaultKey}' + (${'' == defaultValue} ? '' : "/" + '${defaultValue}'));
        displayTxt = displayTxt == '/' ? '' : displayTxt;
        $("#${valueFieldName}").closest("div")
                .find('input[autocomplete="off"]')
                .data("text", displayTxt)
                .val(displayTxt);

    });

</script>
